import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import ExpertRegister from "../views/MyRegister/ExpertRegister.vue";
import CommonRegister from "../views/MyRegister/CommonRegister.vue";
import UserProblem from "../views/PersonalCenter/UserProblem.vue";
import FeedBack from "../views/PersonalCenter/FeedBack.vue";
import Mine from "../views/PersonalCenter/Mine.vue";
import Msg from "../views/PersonalCenter/Msg.vue";
import Login from "../views/PersonalCenter/MyLogin.vue";
import UserRegister from "../views/PersonalCenter/UserRegister.vue";
import { Toast } from "vant";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    redirect: "home",
  },
  {
    path: "/home",
    name: "home",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/MyHome.vue"),
  },
  {
    // 医院概述
    path: "/overview",
    name: "overview",
    component: () =>
      import(
        /* webpackChunkName: "about" */ "../views/HomeChildren/HospitalOverview.vue"
      ),
  },
  {
    // 名医专区
    path: "/famousdoctor",
    name: "famousdoctor",
    component: () =>
      import(
        /* webpackChunkName: "about" */ "../views/HomeChildren/FamousDoctor.vue"
      ),
  },
  {
    path: "/register",
    name: "register",
    redirect: "/register/commom",
    component: () =>
      import(
        /* webpackChunkName: "about" */ "../views/MyRegister/MyRegister.vue"
      ),
    // 使用嵌套路由，来进行定位
    children: [
      {
        path: "commom",
        component: CommonRegister,
      },
      {
        path: "expert",
        component: ExpertRegister,
      },
    ],
  },
  {
    path: "/doctor",
    name: "doctor",
    component: () =>
      import(
        /* webpackChunkName: "about" */ "../views/MyRegister/RegisterDoctor.vue"
      ),
  },
  {
    path: "/confirm",
    // 路由别名，可用在路由跳转
    name: "confirm",
    component: () =>
      import(
        /* webpackChunkName: "about" */ "../views/MyRegister/ConfirmRegister.vue"
      ),
  },
  {
    path: "/user",
    name: "user",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/MyUser.vue"),
  },
  {
    /*动态要闻页面 */
    path: "/problem",
    name: "problem",
    component: UserProblem,
  },
  {
    /*意见反馈页面 */
    path: "/feedback",
    name: "feedback",
    component: FeedBack,
  },
  {
    /*我的消息页面 */
    path: "/msg",
    name: "msg",
    component: Msg,
  },
  {
    /*我的预约页面 */
    path: "/mine",
    name: "mine",
    component: Mine,
  },
  {
    /*登录注册界面 */
    path: "/login",
    name: "login",
    component: Login,
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

router.beforeEach((to, from, next) => {
  if (localStorage.getItem("token") || to.path === "/login") {
    next();
  } else {
    Toast("请先登录！");
    next({ path: "/login", query: { refer: to.fullPath } });
  }
});

export default router;
